package com.qidian.mes.defect.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.defect.service.DetectionService;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.JSONTools;
import com.qidianit.mes.pojo.Defect;
import com.qidianit.mes.pojo.Detection;
import com.qidianit.mes.pojo.ProcessSteps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author 泽华
 * @Description
 * @Time 2024-10-21 19:36
 */
@RestController
@CrossOrigin
@RequestMapping("/detection")

public class DetectionController {
    @Autowired
    private DetectionService detectionService;
    @GetMapping("/list")
    public MESResult list(Detection detection,
                          @RequestParam(defaultValue = "1")int page,
                          @RequestParam(defaultValue = "10") int limit){
        return detectionService.list(detection,page,limit);
    }

    @PostMapping("/save")
    public MESResult save(@RequestBody Detection detection){
        return detectionService.save(detection);
    }


    @DeleteMapping("/delete")
    public MESResult delete(Long detectionId){
        return detectionService.delete(detectionId);
    }

    @PutMapping("/update")
    public MESResult update(@RequestBody Detection detection){
        return detectionService.update(detection);
    }

    //通过id查询这条数据
    @GetMapping("/get")
    public MESResult get(Long detectionId){
        return detectionService.selectById(detectionId);
    }

    //导出
    @GetMapping("/export")
    public MESResult export(Detection detection,int page,int limit) {
        // 查询数据
        MESResult result = detectionService.list(detection, 1, 10);
        PageInfo<Detection> pageInfo = (PageInfo<Detection>) result.getData();
        List<Detection> detections = pageInfo.getList();
        //  通过EasyExcel写数据
        int num = 200;
        try{
            EasyExcel.write("E:\\MES\\Excel\\detection-"+ page + limit + ".xlsx",
                            Detection.class)
                    .sheet("检测项数据")
                    .doWrite(detections);
        }catch (Exception e){
            num = 300;
        }
        return MESResult.success(num,"OK");
    }


}
